//
//  UserAPI.swift
//  Employee
//
//  Created by swordjoy on 2017/3/16.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import Moya
import MBProgressHUD
import Alamofire

class DefaultAlamofireManager: Manager {
  
    static let sharedManager: DefaultAlamofireManager = {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
        configuration.timeoutIntervalForRequest = 15 // as seconds, you can set your request timeout
        configuration.timeoutIntervalForResource = 15 // as seconds, you can set your resource timeout
        configuration.requestCachePolicy = .useProtocolCachePolicy
        return DefaultAlamofireManager(configuration: configuration)
    }()
  
}

let UserProvider = MoyaProvider<UserAPI>(
    manager: DefaultAlamofireManager.sharedManager,
    plugins:[
        NetworkLoggerPlugin(verbose: false)
    ]
)

public enum UserAPI {
    case login(username: String, password: String)
    case orglist()
    case refreshToken()
    case getUserInfo()
    case getUserAddressBook()
    case logout()
    case getModifyPasswordCode(phone: String)
    case verifyCode(phone: String, code: String)
    case resetPassword(phone: String, password: String)
    case setPushAlias()
    case getNotice(page: String)
    
    //获取房号
    case getRegion
    case getUnitHouse(regionId: String)
    
    
}

extension UserAPI: TargetType {
  
    public var headers: [String : String]? {
        return GlobalConfig.httpHeaders
    }
    /// The path to be appended to `baseURL` to form the full `URL`.
    public var path: String {
        switch self {
            case .login:
            return "/oauth2/token"
            case .orglist:
            return "/ms/orglist.json"
            case .refreshToken:
            return "/oauth2/token"
            case .getUserInfo:
            return "/ms/staff.json"//"/mobile/staff.json"
            case .getUserAddressBook:
            return "/mobile/staffs.json"
            case .logout:
            return "/logout.htm"
            case .getModifyPasswordCode:
            return "/mi/getSMSVerificationCode.json"
            case .verifyCode:
            return "/mi/checkIsCorrectCode.json"
            case .resetPassword:
            return "/mi/resetPassWord.json"
            case .setPushAlias:
            return "/ms/staff/setPushAlias.json"
            case .getNotice:
            return "/ms/message/list.json"
            
            case .getRegion:
                return "/mc/property/region/tree.json"
            case .getUnitHouse:
                return "/mc/house/unit.json"
            
            
        }
    }
  
  /// The target's base `URL`.
  public var baseURL: URL {
    switch self {
      case .login, .refreshToken:
        return URL(string: GlobalConfig.BASE_URL)!
    default:
      return URL(string: GlobalConfig.BASE_OUT_URL)!
    }
  }
  
  /// The HTTP method used in the request.
  public var method: Moya.Method {
    switch self {
      case .login, .refreshToken:
        return .post
      case .resetPassword, .setPushAlias():
        return .put
    default: return .get
    }
  }
  
  public var task: Task {
    var parameters: [String: Any] = [:]
    switch self {
    case let .login(username, password):
        parameters = ["client_id": "650c9dda9b1d4fa89a035a382a4f1c04", "client_secret": "b605f96870a24149a14149391deae9d9", "grant_type": "password", "scope": "property", "username": username, "password": password]
    case .orglist():
        parameters = ["access_token": Token.shared.token]
    case .refreshToken():
        parameters = ["client_id": "650c9dda9b1d4fa89a035a382a4f1c04", "client_secret": "b605f96870a24149a14149391deae9d9", "grant_type": "refresh_token", "refresh_token": Token.shared.refreshToken]
    case .getUserInfo:
        parameters = ["access_token": Token.shared.token]
    case .getUserAddressBook:
        parameters = ["access_token": Token.shared.token, "page.pn": "1", "page.size": "1000"]
    case .logout:
        parameters = ["access_token": Token.shared.token]
    case let .getModifyPasswordCode(phone):
        parameters = ["mobile": phone]
    case let .verifyCode(phone, code):
        parameters = ["mobile": phone, "code": code]
    case let .resetPassword(phone, password):
        parameters = ["mobile": phone, "password": password]
    case .setPushAlias:
        parameters = ["access_token": Token.shared.token, "pushAlias": JPUSHService.registrationID() + User.phone]
    case let .getNotice(page):
        parameters = ["access_token": Token.shared.token, "fields": "title,content,sendDate", "page.size": "20", "page.pn": page]
        
    case .getRegion:
        parameters = ["access_token": Token.shared.token, "fields": "id,name","companyId":User.companyId]
    case let .getUnitHouse(regionId):
        parameters = ["access_token": Token.shared.token, "regionId": regionId,"companyId":User.companyId]
        
    }
    return .requestParameters(parameters: parameters.signParame(GlobalConfig.privateKey), encoding: URLEncoding.default)
    }
  
  /// The method used for parameter encoding.
  public var parameterEncoding: ParameterEncoding { return URLEncoding.default }
  
  // 测试使用,暂时用不上
  public var sampleData: Data { return Data() }
}
